package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.*;
import com.example.guiguihomemedical.entity.dto.FulfillmentAddData;
import com.example.guiguihomemedical.entity.dto.FulfillmentviewData;
import com.example.guiguihomemedical.mapper.FulfillmentMapper;
import com.example.guiguihomemedical.mapper.SigningMapper;
import com.example.guiguihomemedical.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class FulfillmentServiceImp implements FulfillmentService {
    @Autowired
    private FulfillmentMapper fulfillmentMapper;
    @Autowired
    private RecordService recordService;
    @Autowired
    private SigningService signingService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private ServicepackService servicepackService;
    @Autowired
    private ServiceitemsService serviceitemsService;
    @Autowired
    private SigningMapper signingMapper;
    @Override
    public List<Fulfillment> findAllFulfillment() {
        return fulfillmentMapper.findAllFulfillment();
    }

    @Override
    public void insertFulfillment(Fulfillment fulfillment) {
        Signing signing = signingMapper.getByIdcardNowServing(fulfillment.getIdcard());
        fulfillment.setSigningid(signing.getId());
        fulfillmentMapper.insertFulfillment(fulfillment);
    }

    @Override
    public List<FulfillmentviewData> findByPrimaryFulfillment(int servicePackagesId, int serviceItemId, String idcard) {
        Signing signing = signingMapper.getByIdcardNowServing(idcard);
        return fulfillmentMapper.findByPrimaryFulfillment(servicePackagesId,serviceItemId,idcard,signing.getId());
    }

    @Override
    public FulfillmentAddData findByPrimaryFulfillmentAdd(int servicePackagesId, int serviceItemId, String idcard) {
        FulfillmentAddData fulfillmentAddData = new FulfillmentAddData();
        Record record = recordService.findByIdcard(idcard);
        fulfillmentAddData.setFulfillmentName(record.getName());
        fulfillmentAddData.setSex(record.getSex());
        fulfillmentAddData.setIdcard(record.getIdcard());
        fulfillmentAddData.setTelephone(record.getPhone());
        Signing signing = signingService.getByIdcardNowServing(idcard);
        Doctor doctor = doctorService.findByDoctorId(signing.getDoctorid());
        fulfillmentAddData.setDoctorName(doctor.getName());
        fulfillmentAddData.setFulfillmentServiceItemName(serviceitemsService.findByIdServiceitems(serviceItemId).getServiceItemsName());
        fulfillmentAddData.setFulfillmentServiceName(servicepackService.getById(servicePackagesId).getName());
        return fulfillmentAddData;
    }
}
